import cv2 as cv


def template_match_demo():
    tpl = cv.imread('roi.jpg',1)
    target = cv.imread('messi5.jpg',1)

    methods = [cv.TM_SQDIFF_NORMED,cv.TM_CCORR_NORMED,cv.TM_CCOEFF_NORMED]
    for md in methods:
        result = cv.matchTemplate(target, tpl, md)
        min_val,max_val,min_local,max_local = cv.minMaxLoc(result)
        if md == cv.TM_SQDIFF_NORMED:
            pt1 = min_local
        else:
            pt1 = max_local
        h,w = tpl.shape[:2]
        pt2 = (pt1[0]+w,pt1[1]+h)
        cv.rectangle(target,pt1,pt2,(0,0,255),2)
        cv.imshow('matched:%s' % md, target)
    cv.waitKey(0)


template_match_demo()



